package com.weng.platform.minder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.exception.GlobalServiceException;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import com.weng.framework.dao.mybatis.service.BaseServiceImpl;
import com.weng.platform.minder.entity.MinderData;
import com.weng.platform.minder.entity.MinderInfo;
import com.weng.platform.minder.mapper.MinderDataMapper;
import com.weng.platform.minder.mapper.MinderInfoMapper;
import com.weng.platform.minder.service.IMinderInfoService;
import com.weng.platform.platform.entity.Column;
import com.weng.platform.platform.mapper.ColumnMapper;
import com.weng.platform.platform.service.IColumnService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;


/**
 * 思维导图信息 服务实现类
 *
 * @author wenghui
 * @date 2020-07-17 20:53:59
 */
@Slf4j
@Service
public class MinderInfoServiceImpl extends BaseServiceImpl<MinderInfoMapper, MinderInfo> implements IMinderInfoService {
    @Autowired
    private MinderInfoMapper minderInfoMapper;
    @Autowired
    private MinderDataMapper minderDataMapper;
    @Autowired
    private ColumnMapper columnMapper;
    @Autowired
    private IColumnService columnService;


    /**
     * 分页查询思维导图信息
     *
     * @return
     */
    @Override
    public IPage<MinderInfo> list(Integer pageNo, Integer pageSize, MinderInfo minderInfo, String ownerId, Boolean isQueryChild) {
        QueryWrapper<MinderInfo> qw = new QueryWrapper<>();
        qw.select("id", "title", "file_url", "column_id", "column_name", "owner_id", "owner_name", "visit_count", "sort_no", "recommend", "update_time");
        if (StringUtils.isNotEmpty(minderInfo.getColumnId())) {
            if (isQueryChild != null && isQueryChild) {
                List<String> childColumnIds = columnService.getAllChildIds(minderInfo.getColumnId());
                qw.in("column_id", childColumnIds);
            } else {
                qw.eq("column_id", minderInfo.getColumnId());
            }
        }

        if (StringUtils.isNotEmpty(minderInfo.getTitle())) {
            qw.like("title", minderInfo.getTitle() + "%");
        }
        qw.eq("owner_id", ownerId);
        qw.orderByAsc("sort_no");
        IPage<MinderInfo> page = new PageModel<>(pageNo, pageSize);
        return minderInfoMapper.selectPage(page, qw);
    }


    /**
     * 获取单个思维导图信息
     *
     * @param id
     * @return
     */
    @Override
    public MinderInfo getById(Serializable id) {
        MinderInfo minderInfo = super.getById(id);
        minderInfo.setContent(getMinderDataByMinderId(id));
        return minderInfo;
    }

    private String getMinderDataByMinderId(Serializable minderId){
        String data = null;
        MinderData minderData = this.minderDataMapper.selectById(minderId);
        if(minderData != null){
            data = minderData.getContent();
        }
        return data;
    }


    /**
     * 新增单个思维导图信息
     *
     * @param minderInfo
     * @return
     */
    @Override
    @Transactional
    public MinderInfo add(MinderInfo minderInfo) throws GlobalServiceException {
        Column column = columnMapper.selectById(minderInfo.getColumnId());
        if (column != null) {
            minderInfo.setColumnName(column.getName());
        }
        minderInfoMapper.insert(minderInfo);
        MinderData minderData = new MinderData();
        minderData.setContent(minderInfo.getContent());
        minderData.setId(minderInfo.getId());
        minderDataMapper.insert(minderData);
        return minderInfo;
    }

    /**
     * 更新单个思维导图信息
     *
     * @param minderInfo
     * @return
     * @throws GlobalServiceException
     */
    @Override
    @Transactional
    public int update(MinderInfo minderInfo) throws GlobalServiceException {
        Column column = columnMapper.selectById(minderInfo.getColumnId());
        if (column != null) {
            minderInfo.setColumnName(column.getName());
        }
        minderInfoMapper.updateById(minderInfo);
        MinderData minderData = this.minderDataMapper.selectById(minderInfo.getId());
        minderData.setContent(minderInfo.getContent());
        this.minderDataMapper.updateById(minderData);
        return 1;
    }


    @Override
    @Transactional
    public ResponseData removeByIds(String ids) {
        String[] minderInfoIds = ids.split(",");
        if(minderInfoIds != null && minderInfoIds.length > 0){
            for(String id: minderInfoIds){
                minderInfoMapper.deleteById(id);
                minderDataMapper.deleteById(id);
            }
        }
        return ResponseData.success();
    }

}
